<?php
/**
 * Session
 * @filesource
 */
/**
 * Namespace
 */
namespace framework\library;
/**
 * Dependencies
 */
use framework\library\interfaces\Session_Interface;
/**
 * Session Class
 *
 * Manages all session activity
 * 
 * @todo Finish Documentation
 * 
 * @author Randy Casburn <randy@plurious.com>
 * @copyright Copyright Plurious Software, all rights reserved
 * @license MIT
 * @license http://www.opensource.org/licenses/mit-license.html
 * @link http://plurious.com/framework/documentation/classes/Front.html
 * @version 2.1
 * 
 * @uses framework\library\interfaces\Session_Interface
 */
class Session implements Session_Interface {

    public $data = array();
    private $sessionTimeout = 1800; // 30 Minutes
    private $sessionCookieExpireTime = 28800; // 8 hours
    private $visitCount;

    public function __construct() {
        $this->init();
        $this->visitCount = (isset($_SESSION['initiated'])) ? $_SESSION['initiated'] : 0;
        // Reset after 5 page loads
        if ($this->visitCount > 4) {
            $this->reset();
        } else {
            // Increment session visit counter
            $this->visitCount++;
            $_SESSION['initiated'] = $this->visitCount;
        }
        setcookie(session_name(), session_id(), $this->sessionCookieExpireTime, '/');
//        $this->resetCookie();
        $this->data = & $_SESSION;
    }

    private function init() {
        ini_set('session.name', 'PLURIOUS');
        ini_set('session.gc_maxlifetime', $this->sessionTimeout);
        ini_set('session.gc_probability', 1);
        ini_set('session.gc_divisor', 1);
        ini_set('session.use_cookies', 'On');
        ini_set('session.use_only_cookies', true);
        ini_set('session.cookie_httponly', true);
        ini_set('session.use_trans_sid', 'Off');
        ini_set('session.save_path', SESSION_DIR );
        session_set_cookie_params($this->sessionCookieExpireTime, '/');
        session_start();
    }

//    public function resetCookie() {
//        // Reset the session cookie timeout
//        if (isset($_COOKIE[session_name()])) {
//            setcookie(session_name(), session_id(), $this->sessionCookieExpireTime, '/');
//        }
//    }

    public function reset($expire = false) {
        if($expire === false){
            $expire = $this->sessionCookieExpireTime;
        }
        session_regenerate_id(true);
        setcookie(session_name(),session_id(), $expire, '/');
        $this->visitCount = 1;
        $_SESSION['initiated'] = $this->visitCount;
    }
    
    public function setExpire($expire){
        $this->sessionCookieExpireTime = $expire;        
    }

    public function kill($sid = null) {
        // Session ID should be stored in session user data when user logs in
        $sid = ($sid)? $sid : $this->get('sid');
        if($sid){
            $path = session_save_path();
            $filename =  $path.'/sess_'.$sid;
            if(file_exists($filename)){
                unlink($filename);
            }
        }
        $this->data = null;
        session_destroy();
        setcookie(session_name(), '', time() - 3600, '/');
    }
    
    public function removeUser(){
        if(session_id()){
            $_SESSION['userData'] = null;
        }
        $this->reset();
    }
    
    public function setUserData($data){
        if(session_id()){
            $_SESSION['userData'] = $data;
        }
    }
    
    public function getUserData($key = ''){
        if(session_id() && isset($_SESSION['userData'])){
            return ($key)? $_SESSION['userData'][$key] : $_SESSION['userData'];
        }
        return null;
    }
    
    public function set($key, $data){
        if(session_id()){
            $_SESSION[$key] = $data;
        }
    }

    public function get($key){
        if(session_id() and isset($_SESSION[$key])){
            return $_SESSION[$key];
        }
        return false;
    }
    
    public function killAll(){
        $files = glob(session_save_path().'/*');
        foreach($files as $file){
            unlink($file);
        }
    }

}

?>